﻿@using Bonobo.Git.Server.Extensions
@using System.Web.Mvc.Html
@using Bonobo.Git.Server.Models
@using Bonobo.Git.Server.Data

@model Dictionary<string, RepositoryDetailModel[]>
@{
    ViewBag.Title = Resources.Repository_Index_Title;
}

@functions
{
    string GetNameToDisplay(string name, RepositoryDetailStatus status)
    {
        switch (status)
        {
            case RepositoryDetailStatus.Valid:
                return name;

            case RepositoryDetailStatus.Missing:
                return name + " (Missing)";

            case RepositoryDetailStatus.Unknown:
            default:
                return name + " (Status Unknown)";
        }
    }
}

<div class="pure-g">
    <div class="pure-u-2-5">
        <h1>
            @Resources.Repository_Index_Title
        </h1>
    </div>
    <div class="pure-u-3-5 add">
        @{
            var sortGroupQuery = Request.QueryString["sortGroup"];

            var sortGroup = new
            {
                sortGroup = string.IsNullOrEmpty(sortGroupQuery) ? "DESC" : ((sortGroupQuery.Equals("ASC") ? "DESC" : "ASC"))
            };
        }

        <a class="pure-button pure-button-primary" href="@Url.Action("Index", sortGroup)"><i class="fa fa-sort"></i> @Resources.Repository_Index_SortGroup</a>

        @if (User.IsInRole(Definitions.Roles.Administrator) || UserConfiguration.Current.AllowUserRepositoryCreation)
        {
            <a class="pure-button pure-button-primary" href="@Url.Action("Create")"><i class="fa fa-plus-circle"></i> @Resources.Repository_Index_CreateNew</a>
        }
        @if (User.IsInRole(Definitions.Roles.Administrator))
        {
            <form action="@Url.Action("Rescan", new { id = "x" })" method="post" class="inline-form"><button type="submit" class="pure-button pure-button-primary"><i class="fa fa-refresh"></i>@Resources.Repository_Rescan_Directory</button></form>
        }
    </div>
</div>

@{
    object RepoNotFoundObj;
    bool RepoNotFound = false;
    if (TempData.TryGetValue("RepositoryNotFound", out RepoNotFoundObj))
    {
        RepoNotFound = (bool)RepoNotFoundObj;
    }
    if (RepoNotFound)
    {
        <div class="pure-u-2-5">
            @Html.Partial("~/Views/Shared/_ItemNotFound.cshtml")
        </div>
    }
}
@if (TempData["CreateSuccess"] as bool? == true)
{
    <div class="summary-success">
        <p>@Resources.Repository_Create_Success</p>
                <p>@Html.ActionLink(string.Format(@Resources.Repository_Create_Success_Go_To, TempData["SuccessfullyCreatedRepositoryName"].ToString()), "Detail", new { id = TempData["SuccessfullyCreatedRepositoryId"].ToString() })</p>
    </div>
}
@if (TempData["CloneSuccess"] as bool? == true)
{
    <div class="summary-success">
        <p>@Resources.Repository_Clone_Success</p>
    </div>
}
@if (TempData["DeleteSuccess"] as bool? == true)
{
    <div class="summary-success">
        <p>@Resources.Repository_Delete_Success</p>
    </div>
}

@using (Html.BeginForm(null, null, FormMethod.Post, new { @class = "pure-form" }))
{
    <p>
        @Html.TextBox("SearchString")
        <input type="submit" value="@Resources.Search" class="pure-button pure-button-primary" />
    </p>
}

<div class="pure-g">
    @{
        foreach (KeyValuePair<string, RepositoryDetailModel[]> model in Model)
        {
            bool emptyGroup = string.IsNullOrEmpty(model.Key);

            if (!emptyGroup)
            {
                <div class="pure-u-1">
                    <h2 class="group">
                        <span class="toggle" data-group="@(model.Key)"></span>
                        @(model.Key)
                    </h2>
                </div>
            }
            // https://dusted.codes/making-font-awesome-awesome-using-icons-without-i-tags
            <div class="pure-u-1">
                @{
                    var permitted = new WebGrid(source: model.Value, defaultSort: "Name", rowsPerPage: 15);


                    var columns = new List<WebGridColumn>();
                    columns.Add(permitted.Column(
                        format: item => Html.Raw(item.Logo.Exists ? string.Format("<img src='data:image/png;base64,{0}' class='repo-logo' />", item.Logo.Base64Image) : ""),
                        style: "logo"
                    ));
                    columns.Add(permitted.Column("Name",
                        header: typeof(RepositoryDetailModel).GetDisplayValue("Name"),
                        format: (item) => Html.ActionLink(
                                GetNameToDisplay((string)item.Name, (RepositoryDetailStatus)item.Status),

                                "Detail",
                                new { id = item.Id },
                                new
                                {
                                    @class = "RepositoryName detail" + (item.Logo.Exists ? " customLogo" : ""),
                                    @id = "repo_" + item.Id,
                                }
                                )));

                    if (User.IsInRole(Definitions.Roles.Administrator))
                    {
                        columns.Add(permitted.Column(
                            format: (item) =>
                            {
                                var repo = (RepositoryDetailModel)item.Value;
                                var anonColor = repo.AllowAnonymous ? "rgba(0, 120, 230, 1)" : "lightblue";
                                var anonTitle = Resources.Repository_Detail_Anonymous + ": " + (repo.AllowAnonymous ? Resources.Yes : Resources.No);

                                var pushSetting = UserConfiguration.Current.AllowAnonymousPush;
                                if(repo.AllowAnonymousPush != RepositoryPushMode.Global){
                                    pushSetting = repo.AllowAnonymousPush == RepositoryPushMode.Yes;
                                }
                                
                                var pushIcon = repo.AllowAnonymousPush == RepositoryPushMode.Global ? "fa-globe" : "fa-sign-in";
                                var pushTitle = Resources.Repository_Detail_AllowAnonymousPush + ": " + (pushSetting ? Resources.Yes : Resources.No); 
                                var pushColor = pushSetting ? "rgba(0, 120, 230, 1)" : "lightblue";
                                var ret = Html.Raw("<span class='fa-fw'>").ToHtmlString();
                                ret += Html.Raw("<div class='fa fa-sign-out' style='display: block; color: " + anonColor + "' title='" + anonTitle + "'></div>").ToHtmlString();
                                ret += Html.Raw("<div class='fa " + pushIcon + "' style='display: block; color: " + pushColor + "' title='" + pushTitle + "'></div>").ToHtmlString();
                                ret += Html.Raw("</span>").ToHtmlString();
                                return Html.Raw(ret);
                            }
                        ));
                    }

                    columns.Add(permitted.Column("Description", typeof(RepositoryDetailModel).GetDisplayValue("Description"), format: (item) => ((item.Description != null && ((string)item.Description).Length > 100)) ? ((string)item.Description).Substring(0, 100) + " ... " : item.Description, style: "description"));
                    columns.Add(permitted.Column(format: (item) => Html.Raw("<button class=\"pure-button pure-button-primary copy-to-clip-button\" title=\"" + Resources.Repository_Git_Url_Copy + "\" data-clipboard-text=\"" + Html.Encode(item.GitUrl) + "\"><i class=\"fa fa-copy\" ></i></button>"), style: "action"));
                    columns.Add(permitted.Column(format: (item) => Html.Raw("<button class=\"pure-button pure-button-primary copy-to-clip-button\" title=\"" + Resources.Repository_Personal_Git_Url_Copy + "\" data-clipboard-text=\"" + Html.Encode(item.PersonalGitUrl) + "\"><i class=\"fa fa-user\"></i><i class=\"fa fa-copy\"></i></button>"), style: "action"));
                    columns.Add(permitted.Column(format: (item) => Html.ActionLink(" ", "Edit", new { id = item.Id }, new { @class = "edit", title = Resources.Grid_Edit }), style: "action"));
                    columns.Add(permitted.Column(format: (item) => Html.ActionLink(" ", "Delete", new { id = item.Id }, new { @class = "delete", title = Resources.Grid_Delete }), style: "action"));

                    permitted.Columns(columns.ToArray());
                    @permitted.GetHtml(
                        tableStyle: "pure-table repositories",
                        columns: columns,
                        htmlAttributes: new { @Id = "Repositories" }
                        )
                }
            </div>
        }
    }
</div>

@section scripts{
    <script src="~/Scripts/RepositoryGroupToggle.js" defer="defer"></script>
    <script>new Clipboard('.copy-to-clip-button');</script>
}
